#include "tmmysqlctrl.h"
#include "tmglobaldef.h"

TMMySQLCtrl::TMMySQLCtrl(QObject *parent) : QObject(parent)
{
    // mysql heart timer
    QTimer *tmHeart = new QTimer();
    connect(tmHeart, SIGNAL(timeout()), this, SLOT(slot_timer_mysqlheart()), Qt::QueuedConnection );
    tmHeart->start(1000*60*60); // 1小时查询一次,防止Mysql长链接断开
}

bool TMMySQLCtrl::connect_mysql(QString m_strIp)
{
    pr_dbMysql = QSqlDatabase::addDatabase("QMYSQL", "tami");
    pr_dbMysql.setDatabaseName("tami");
    pr_dbMysql.setHostName(m_strIp);
    pr_dbMysql.setUserName("root");
    pr_dbMysql.setPassword("tamigroup");
    pr_dbMysql.setConnectOptions("MYSQL_OPT_CONNECT_TIMEOUT=3;");
    if (!pr_dbMysql.open()) {
        qDebug() << "Connect to MySql error: "<< pr_dbMysql.lastError().text();
        return false;
    }
    QSqlQuery query(pr_dbMysql);
    query.exec("SELECT 1;");
    if(query.size()>0){
        return true;
    }
    return false;
}

void TMMySQLCtrl::saveUserState(QString userName, State s)
{
    QString strSQL = QString("UPDATE t_user SET state=%1 WHERE user_name='%2';").arg(s).arg(userName);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    qDebug()<<"strSQL:"<<strSQL;
}

void TMMySQLCtrl::saveRobotInfo(QString robotMac, QString password, QString sn, QString version, State s, QString avAdress)
{
    QString strSQL = QString("UPDATE t_robot SET password='%1',sn='%2',version='%3',state=%4"
                             ",av_adress='%5' WHERE robot_name='%6';")
                            .arg(password).arg(sn).arg(version).arg(s).arg(avAdress).arg(robotMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
}

void TMMySQLCtrl::saveCurUserName(QString strMac, QString strName)
{
    QString strSQL = QString("UPDATE t_robot SET cur_conn_user='%1' WHERE robot_name='%2';").arg(strName).arg(strMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
}

QString TMMySQLCtrl::queryPwdByName(QString strName)
{
    QString strSQL = QString("SELECT password FROM t_user WHERE user_name='%1';").arg(strName);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    if(query.size()<1){
        return "";
    }
    query.next();
    return query.value(0).toString();
}

NetWorkRobot::CallBackPrx *TMMySQLCtrl::queryCallBackPrx(QString robotMac)
{
    QString strSQL = QString("SELECT callbackptr FROM t_robot WHERE robot_name='%1';").arg(robotMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    if(query.size()<1){
        return NULL;
    }
    query.next();
    return (NetWorkRobot::CallBackPrx*)query.value(0).toInt();
}

QString TMMySQLCtrl::queryRobotInfo(QString robotMac)
{
    QString strSQL = QString("SELECT * FROM t_robot WHERE robot_name='%1';").arg(robotMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    QString robotInfo;
    if(query.size()<1){
        return "";
    }
    query.next();
    robotInfo = QString("{\"robotMac\":\"%1\",\"sn\":\"%2\",\"version\":\"%3\",\"AVadress\":\"%4\",\"state\":%5}").arg(robotMac).arg(query.value("sn").toString())
            .arg(query.value("version").toString()).arg(query.value("av_adress").toString()).arg(query.value("state").toInt());
    return robotInfo;
}

bool TMMySQLCtrl::robotIsInMySQL(QString robotMac)
{
    QString strSQL = QString("SELECT * FROM t_robot WHERE robot_name='%1';").arg(robotMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    if(query.size()<1){
        return false;
    }
    return true;
}

void TMMySQLCtrl::queryRobotList(QString userName,Ice::StringSeq &infoList)
{
    QString strSQL = QString("SELECT robot_name FROM t_related WHERE user_name='%1';").arg(userName);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);
    while(query.next()){
        infoList.push_back(queryRobotInfo(query.value("robot_name").toString()).toUtf8().toStdString());
    }
}

void TMMySQLCtrl::updateRobotState(QString strMac, State s)
{
    QString strSQL = QString("UPDATE t_robot SET state=%1 WHERE robot_name='%2';").arg(s).arg(strMac);
    QSqlQuery query(pr_dbMysql);
    query.exec(strSQL);

}

void TMMySQLCtrl::slot_timer_mysqlheart()
{
    QSqlQuery query(pr_dbMysql);
    query.exec("SELECT 1;");
    if( query.size()<1 ){
        g_pLog->log_D("Ping Mysql DB ret is Null! Try Open...");
        qDebug()<<"Ping Mysql DB ret is Null! Try Open:"<<pr_dbMysql.open();
    }

}

